<?php
/**
 * Created by PhpStorm.
 * User: colin
 * Date: 15-4-1
 * Time: 21:27
 */

namespace common\models;

use common\error\ErrorCode;
use yii;
use yii\base\Model;

class BaseModel extends Model
{
    use \common\error\ReturnErrorTrait;

    public function paramsGetValidate()
    {
        if (!Yii::$app->request->isGet) {
            return $this->setAndReturn(ErrorCode::ERR_REQUEST_MUST_GET);
        }
        if ($this->load([$this->formName() => Yii::$app->request->get()])
            && $this->validate()) {
            return true;
        }
        if ($this->getErrCode()) {
            return false;
        }
        return $this->setAndReturn(ErrorCode::ERR_PARAMS_ERR,$this->getAllFirstErrorMessage());
    }

    public function paramsPostValidate()
    {
        if (!Yii::$app->request->isPost) {
            return $this->setAndReturn(ErrorCode::ERR_REQUEST_MUST_POST);
        }
        if ($this->load([$this->formName() => Yii::$app->request->post()])
            && $this->validate()) {
            return true;
        }
        if ($this->getErrCode()) {
            return false;
        }
        return $this->setAndReturn(ErrorCode::ERR_PARAMS_ERR,$this->getAllFirstErrorMessage());
    }
}